import {
  areaList
} from '@vant/area-data/index';
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
import {isMobile} from "../../util/util";
import {Classify} from "../../models/classify";
import {Message} from "../../models/message";
import {showToast} from "../../util/ui";

Page({
  data: {
    userName: null,
    phoneNumber: null,
    areaList,
    area: null,
    //是否弹起选择地区
    showVanArea: false,
    // 是否弹起选择分类
    showType: false,
    describe: null,
    type: null,
    // money: null,
    autoSize: {
      maxHeight: '220px',
      minHeight: '90px'
    },
    query: {
      pageNum: 1,
      pageSize: 30
    },
    typeList: [],
    showLoading: true
  },
  async onLoad(options) {
    const classifyRes = await Classify.doApiGetClassifyList()
    if (classifyRes.code !== 200) {
      showToast(classifyRes.msg)
      return
    }
    const list = classifyRes.rows.map((item) => {
      return item.name
    })
    this.setData({
      typeList: list,
      showLoading: false
    })
  },
  bindTypeInput() {
    this.setData({
      showType: true
    })
  },
  onTypeCancel() {
    this.setData({
      showType: false
    })
  },
  onTypeChange(event) {
    const {value} = event.detail;
    this.setData({
      type: value,
      showType: false
    })
  },
  // 点击选择地区input
  bindAreaInput() {
    this.setData({
      showVanArea: true
    })
  },
  //点击完成area
  confirmArea(obj) {
    const values = obj.detail.values
    const codesArray = [] //地区codes
    const areaArray = [] //地区名
    let tempArea = ''
    values.forEach(value => {
      areaArray.push(value.name)
      codesArray.push(value.code)
    })

    areaArray.forEach(a => {
      tempArea += `${a} `
    })
    this.setData({
      showVanArea: false,
      area: tempArea
    })
  },
  //点击取消area
  cancelArea() {
    this.setData({
      showVanArea: false
    })
  },
  async onsubmit(event) {
    const {
      userName,
      phoneNumber,
      describe,
      type
    } = event.detail.value
    if (userName === '' || phoneNumber === '' || type === '' || describe === '') {
      Toast('请填写完整信息')
      return
    }
    if (userName.length < 2 || userName.length > 6) {
      Toast('用户名字符在2-6之间')
      return;
    }
    if (!isMobile(phoneNumber)) {
      Toast('请填写正确的电话号码')
      return;
    }
    this.setData({
      showLoading: true
    })
    const data = {}
    data.name = userName
    data.phone = phoneNumber
    data.caseType = type
    data.content = describe
    data.isHandle = '0'
    const res = await Message.doApiAddMessage(data)
    if (+res.code === 200) {
      Toast('提交成功')
      this.setData({
        userName: null,
        phoneNumber: null,
        describe: null,
        type: null,
        showLoading: false
      })
    }
  }
})
